package org.example.ychdp.controller;

import jakarta.servlet.http.HttpSession;
import org.example.ychdp.entity.*;
import org.example.ychdp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Controller
public class dpController {

    @Autowired
    private ConcertService concertService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private CommentService commentService;
    @RequestMapping("/dp")
    public String dp(@RequestParam(required = false, defaultValue = "") Integer id,
                     Model model) {
        Concert concert = concertService.selectByPrimaryKey(id);
        System.out.println(concert);
        model.addAttribute("concert", concert);

        List<Ticket> tickets = ticketService.buyTicket(concert.getConcertid());
        model.addAttribute("tickets", tickets);

        List<Comment> comments = commentService.getCommentsByConcerId(id);
        model.addAttribute("comments", comments);

        return "dp";
    }

    @Autowired
    private OrderService orderService;
    @RequestMapping("/order")
    public String dpList(@RequestParam("concertid") Integer concertid,
                         @RequestParam("ticketid") Integer ticketid,
                         HttpSession session,
                         Model model) {
        if (session.getAttribute("user")==null){
            String errorMsg = "您未登录，请登录";
            model.addAttribute("errorMsg",errorMsg);
            return "login";
        }
        Concert concert = concertService.selectByPrimaryKey(concertid);
        model.addAttribute("concert", concert);

        Ticket ticket = ticketService.getTicket(ticketid);
        model.addAttribute("ticket", ticket);
        return "order";
    }

    @Autowired
    private SeatService seatService;
    @Autowired
    UserService userService;

    @RequestMapping("/createOrder")
    public String CreateOrder(User user,
                          @RequestParam("concertid") Integer concertid,
                          @RequestParam("ticketid") Integer ticketid,
                          @RequestParam("orderid") String orderid,
                          HttpSession session,
                          Model model) {
        User user1 = (User) session.getAttribute("user");
        if (user1==null){
            String errorMsg = "您未登录，请登录";
            model.addAttribute("errorMsg",errorMsg);
            return "login";
        }

        Concert concert = concertService.selectByPrimaryKey(concertid);
        Ticket ticket = ticketService.getTicket(ticketid);
        Seat seat = new Seat();
        seat.setSeatarea(ticket.getSeatarea());
        seat.setTicketid(ticket.getTicketid());
        seat.setPrice(ticket.getPrice());
        seat.setUserid(user1.getUserid());
        Seat seat1 = seatService.insert(seat);
        ticketService.update(ticket);

        Order order = new Order();
        order.setUserid(user1.getUserid());
        order.setConcertid(concertid);
        order.setTicketid(ticketid);
        order.setSeatid(seat1.getSeatid());
        Order order1 = orderService.insertOrder(order);

        session.setAttribute("order", order1);

        userService.updateUser(user);
        return "createOrder";
    }
    @RequestMapping("/orderDo")
    public String OrderDo(@RequestParam("orderid") String orderid,
                          HttpSession session,
                          Model model) {
        User user = (User) session.getAttribute("user");
        if (user==null){
            String errorMsg = "您未登录，请登录";
            model.addAttribute("errorMsg",errorMsg);
            return "login";
        }

        Order order = orderService.selectById(orderid);
        Concert concert = concertService.selectByPrimaryKey(order.getConcertid());
        Ticket ticket = ticketService.getTicket(order.getTicketid());
        Seat seat = seatService.selectByPrimaryKey(order.getSeatid());
        if (order.getPaymenttime()==null){
            orderService.update(order);
            session.setAttribute("order", order);
            return "createOrder";
        }


        model.addAttribute("concert", concert);
        model.addAttribute("ticket", ticket);
        model.addAttribute("order", order);
        model.addAttribute("seat", seat);
        model.addAttribute("user", user);

        return "orderDo";
    }
    @RequestMapping("/orderList")
    public String orderList(
                      HttpSession session,
                      Model model) {
        User user = (User) session.getAttribute("user");
        List<Order> orders = orderService.selectByUserId(user.getUserid());
        model.addAttribute("orders", orders);
        return "orderList";
    }
}
